package com.cszjzx.member.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.cszjzx.member.po.Field;
import com.cszjzx.member.service.FieldService;
import com.cszjzx.member.service.FieldServiceImpl;
//import com.cszjzx.member.service.StageService;
//import com.cszjzx.member.service.StageServiceImpl;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class FieldAction extends ActionSupport{
	private int fieldId;
	private int stageId;
	private String chinese;
	private String defaultValue;
	private Field field;
	private String message;
	HttpServletRequest request = ServletActionContext.getRequest();
	private FieldService fieldService = new FieldServiceImpl();
	//private StageService stageService = new StageServiceImpl();
	

	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}

	public Field getField() {
		return field;
	}
	public void setField(Field field) {
		this.field = field;
	}

	public String getChinese() {
		return chinese;
	}
	public void setChinese(String chinese) {
		this.chinese = chinese;
	}

	public String getDefaultValue() {
		return defaultValue;
	}
	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public int getFieldId() {
		return fieldId;
	}
	public void setFieldId(int fieldId) {
		this.fieldId = fieldId;
	}

	public int getStageId() {
		return stageId;
	}
	public void setStageId(int stageId) {
		this.stageId = stageId;
	}

	@SuppressWarnings("unchecked")
	public String add(){
		List<Field> fields = (List<Field>)request.getSession().getAttribute("fields");
		Field f = new Field();
		for(int i=0;i<fields.size();i++){
			if(fields.get(i).getChinese().equals(chinese)){
				message = "该字段名称已存在，请重新输入！";
				return "addError";
			}
		}
		f.setChinese(chinese);
		if(defaultValue.equals("")){
			defaultValue = null;
		}
		f.setDefaultValue(defaultValue);
		f.setId(fields.get(fields.size()-1).getId()+1);
		f.setName("index"+f.getId());
		f.setStage(stageId);
		if(fieldService.add(f)){
			fields = fieldService.getFieldList();
			request.getSession().setAttribute("fields", fields);
			return "addSuccess";
		}
		else {
			message = "字段添加失败，请重试！";
			field = f;
			return "addError";
		}
	}
	
	@SuppressWarnings("unchecked")
	public String showEdit(){
		List<Field> fields = (List<Field>)request.getSession().getAttribute("fields");
		for(int i=0;i<fields.size();i++){
			if(fields.get(i).getId()==fieldId){
				field = fields.get(i);
			}
		}
		return "showEdit";
	}
	
	@SuppressWarnings("unchecked")
	public String edit(){
		List<Field> fields = (List<Field>)request.getSession().getAttribute("fields");
		Field f = new Field();
		f.setChinese(chinese);
		for(int i=0;i<fields.size();i++){
			if(fields.get(i).getChinese().equals(chinese)&&fields.get(i).getId() != fieldId){
				message = "该字段名称已存在，请重新输入！";
				field = f;
				return "editError";
			}
			if(fields.get(i).getId() == fieldId){
				f = fields.get(i);
			}
		}
		f.setChinese(chinese);
		if(defaultValue.equals("")){
			defaultValue = null;
		}
		f.setDefaultValue(defaultValue);
		if(fieldService.edit(f)){
			fields = fieldService.getFieldList();
			request.getSession().setAttribute("fields", fields);
			return "editSuccess";
		}
		else {
			message = "字段修改失败，请重试！";
			field = f;
			return "editError";
		}
	}
	
	@SuppressWarnings("unchecked")
	public String delete(){
		Field f = new Field();
		List<Field> fields = (List<Field>)request.getSession().getAttribute("fields");
		for(int i=0;i<fields.size();i++){
			if(fields.get(i).getId() == fieldId){
				f = fields.get(i);
			}
		}
		fieldService.delete(f);
		fields = fieldService.getFieldList();
		request.getSession().setAttribute("fields", fields);
		message= "删除成功！";
		return "deleteSuccess";
	}
}
